﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    internal class ComponentsViewModel:INotifyPropertyChanged
    {
        private static ComponentsViewModel mInstance = null;
        public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<ComponentViewModel> Components { get; set; }
        private ComponentViewModel mComponent = null;

        public ComponentViewModel Component
        {
            get { return mComponent; }
            set
            {
                mComponent = value;
                RaisePropertyChanged("Component");
            }
        }

        private ComponentsViewModel()
        {
            if (null == Components) Components = new ObservableCollection<ComponentViewModel>();
        }
        public static ComponentsViewModel GetInstance()
        {
            if (null == mInstance) mInstance = new ComponentsViewModel();
            return mInstance;
        }

        public XmlElement Export(XmlDocument doc)
        {
            XmlElement _element = null;
            if (null == doc) return _element;
            if (0 < Components.Count)
            {
                _element = doc.CreateElement("Components");
                foreach (ComponentViewModel _component in Components) _element.AppendChild(_component.Export(doc));
            }
            return _element;
        }
        public void Import(XmlElement element)
        {
            if (0 < Components.Count) Components.Clear();
            if (null == element) return;
            if ("Components" == element.Name)
            {
                if (element.HasChildNodes)
                {
                    foreach (XmlElement _child in element.ChildNodes)
                    {
                        ComponentViewModel _component = new ComponentViewModel();
                        _component.Import(_child);
                        Components.Add(_component);
                    }
                }
            }
        }

        private void RaisePropertyChanged(string strPropertyName)
        {
            PropertyChangedEventHandler _handler = this.PropertyChanged;
            if (null != _handler) _handler(this, new PropertyChangedEventArgs(strPropertyName));
        }
    }
}
